package org.example.common.response;

import lombok.Getter;

import java.io.Serializable;
import java.util.Objects;

public class ApiResult<T> implements Serializable {

    @Getter
    public Integer code;

    @Getter
    public T data;

    @Getter
    public String msg;

    public ApiResult() {
    }

    public ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(ApiResultCode res, T data) {
        this.code = res.getCode();
        this.msg = res.getMsg();
        this.data = data;
    }


    public boolean isSuccess() {
        return Objects.equals(this.code, ApiResultCode.SUCCESS.getCode());
    }

    public static <T> ApiResult<T> success() {
        return ApiResult.res(ApiResultCode.SUCCESS);
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ApiResultCode.SUCCESS, data);
    }

    public static <T> ApiResult<T> error() {
        return ApiResult.res(ApiResultCode.FAIL);
    }

    public static <T> ApiResult<T> error(T data) {
        return new ApiResult<>(ApiResultCode.FAIL, data);
    }

    public static <T> ApiResult<T> error(String msg) {
        return new ApiResult<>(ApiResultCode.FAIL.getCode(), msg);
    }

    public static <T> ApiResult<T> res(int code, String msg) {
        return new ApiResult<>(code, msg);
    }

    public static <T> ApiResult<T> error(int code, String msg) {
        return new ApiResult<>(code, msg);
    }


    public static <T> ApiResult<T> res(ApiResultCode res) {
        return new ApiResult<>(res.getCode(), res.getMsg());
    }


    public static ApiResult<Object> error(int code, String msg, Object data) {
        return new ApiResult<>(code, msg, data);
    }
}
